package com.pandora.credit.web;

import com.pandora.common.parameter.annotation.RequestData;
import com.pandora.common.util.Constants;
import com.pandora.credit.dto.request.*;
import com.pandora.credit.dto.response.*;
import com.pandora.credit.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author developer
 */
@RestController
public class UserController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AccountService accountService;
    @Autowired
    private SmsServcie smsServcie;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private TerminalService terminalService;
    @Autowired
    private MetaDataService metaDataService;
    @Autowired
    private VersionService versionService;
    @Autowired
    private AppUpgradeService appUpgradeService;

    /**
     * 访问令牌
     */
    @RequestMapping(value = "access_token", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public AccessTokenResponse requestAccessToken(@RequestBody AccessTokenRequest req) {
        return accountService.requestAccessToken(req);
    }
    @RequestMapping(value = "access_token", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public AccessTokenResponse requestAccessTokenForm(@RequestData AccessTokenRequest req) {
        return accountService.requestAccessToken(req);
    }

    /**
     * 短信验证码请求/验证
     * @param response response
     *                     1. 1分钟内同一app_id内同一手机号不可重复发送短信验证码
     *                     2. 短信验证码验证通过，会分配一临时Authorization Token，以mobile=Authentication Token值对存储，并记录当前用户为“临时用户”
     */
    @RequestMapping(value = "sms_code", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Object requestSmsCode(@RequestHeader(value = "access_token", required = false, defaultValue = "") String accessToken,
                                 @RequestBody SmsCodeRequest smsCodeRequest,
                                 HttpServletResponse response) {
        Object ret = smsServcie.handleRequest(smsCodeRequest, accessToken);
        if (ret == null) {
            response.setStatus(204);
        }
        return ret;
    }
    @RequestMapping(value = "sms_code", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public Object requestSmsCodeForm(@RequestHeader(value = "access_token", required = false, defaultValue = "") String accessToken,
                                 @RequestData SmsCodeRequest smsCodeRequest,
                                 HttpServletResponse response) {
        Object ret = smsServcie.handleRequest(smsCodeRequest, accessToken);
        if (ret == null) {
            response.setStatus(204);
        }
        return ret;
    }


    /**
     * 设置/重置密码
     */
    @RequestMapping(value = "password", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public AuthorizationTokenResponse resetPassword(@RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
                                                    @RequestBody ResetPasswordRequest resetPasswordRequest) {
        return customerService.resetPassword(resetPasswordRequest, authorizationToken);
    }
    @RequestMapping(value = "password", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public AuthorizationTokenResponse resetPasswordForm(@RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
                                                    @RequestData ResetPasswordRequest resetPasswordRequest) {
        return customerService.resetPassword(resetPasswordRequest, authorizationToken);
    }

    /**
     * 实名认证
     * @param authorizationToken Authorization 授权Token
     */
    @RequestMapping(value = "certification", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public AuthorizationTokenResponse requestCertification(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @RequestBody CertificationRequest certificationRequest) {
        return customerService.requestCertification(certificationRequest, authorizationToken);
    }
    @RequestMapping(value = "certification", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public AuthorizationTokenResponse requestCertificationForm(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @RequestData CertificationRequest certificationRequest) {
        return customerService.requestCertification(certificationRequest, authorizationToken);
    }

    /**
     * 登录
     * @param accessToken access_token
     */
    @RequestMapping(value = "login", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    private AuthorizationTokenResponse login(
            @RequestHeader(value = "access_token", required = false, defaultValue = "") String accessToken,
            @RequestBody LoginRequest loginRequest) {
        return customerService.login(loginRequest, accessToken);
    }
    @RequestMapping(value = "login", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    private AuthorizationTokenResponse loginForm(
            @RequestHeader(value = "access_token", required = false, defaultValue = "") String accessToken,
            @RequestData LoginRequest loginRequest) {
        return customerService.login(loginRequest, accessToken);
    }

    /**
     * 获取protocal
     */
    @RequestMapping(value = "protocal", method = RequestMethod.GET)
    public ProtocolResponse requestProtocol() {
        String value = metaDataService.getByType(Constants.META_DATA_TYPE_PROTOCAL);
        return new ProtocolResponse(value);
    }

    /**
     * 获取平台+版本开关
     */
    @RequestMapping(value = "switch", method = RequestMethod.GET)
    public SwitchResponse switchFunc(@RequestHeader(value = "User-Agent", required = false, defaultValue = "") String ua) {
        return versionService.requestSwitch(ua);
    }

    /**
     * app版本升级
     */
    @RequestMapping(value = "upgrade", method = RequestMethod.GET)
    public UpgradeInfoResponse getUpgradeInfo(@RequestData UpgradeRequest req) {
        return appUpgradeService.getUpgradeInfo(req);
    }

    /**
     * 上报终端信息
     * @param accessToken        access_token
     * @param authorizationToken Authorization 授权Token
     */
    @RequestMapping(value = "terminal", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public Object uploadTerminalInfo(@RequestHeader(value = "access_token", required = false, defaultValue = "") String accessToken,
                                         @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
                                         @RequestBody TerminalInfoRequest terminalInfoRequest) {
        terminalService.uploadTerminalInfo(terminalInfoRequest, accessToken, authorizationToken);
        return null;
    }
    @RequestMapping(value = "terminal", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public Object uploadTerminalInfoForm(@RequestHeader(value = "access_token", required = false, defaultValue = "") String accessToken,
                                     @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
                                     @RequestData TerminalInfoRequest terminalInfoRequest) {
        terminalService.uploadTerminalInfo(terminalInfoRequest, accessToken, authorizationToken);
        return null;
    }

    /**
     * 图形验证码
     */
    @RequestMapping(value = "captcha", method = RequestMethod.GET)
    public GetCaptchaResponse doGetCaptcha(@RequestHeader(value = "access_token", required = false, defaultValue = "") String accessToken) {
        return appUpgradeService.doGetCaptcha(accessToken);
    }

}
